/*
 * Copyright (c) 2011 Ball State University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package edu.bsu.android.gtv.apps.bconnected.parser;

import org.xml.sax.Attributes; 
import org.xml.sax.SAXException; 
import org.xml.sax.helpers.DefaultHandler;  

import edu.bsu.android.gtv.apps.bconnected.objects.Person;

/**
 *  Handles the XML input from the login handler - in order to get the XML stream back from the WindowsAuth .NET application, it has to be a more
 *  simplified version of the parser factory. 
 *  
 * @author Kyle Parker, University Computing Services
 */
public class PersonHandler extends DefaultHandler { 
	private boolean in_bsu_id = false; 
	private boolean in_first_name = false; 
	private boolean in_login_guid = false; 
	
	private Person person = new Person(); 

	public Person getParsedData() { 
		 return this.person; 
	} 
      
	@Override 
	public void startDocument() throws SAXException { 
		this.person = new Person();  
	} 

     @Override 
     public void endDocument() throws SAXException  { }

     @Override 
     public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 
          if (localName.equals("bsuid")) { 
         	 this.in_bsu_id = true; 
          } else if (localName.equals("firstName"))  { 
         	 this.in_first_name = true; 
          } else if (localName.equals("loginGuid"))  { 
         	 this.in_login_guid = true; 
          }
     } 
      
     @Override 
     public void endElement(String namespaceURI, String localName, String qName) throws SAXException { 
         if (localName.equals("bsuid")) { 
        	 this.in_bsu_id = false; 
         } else if (localName.equals("firstName"))  { 
        	 this.in_first_name = false; 
         } else if (localName.equals("loginGuid"))  { 
        	 this.in_login_guid = false; 
         }
     } 
      
     /** Gets be called on the following structure: 
      * <tag>characters</tag> */ 
     @Override 
	public void characters(char ch[], int start, int length) { 
          if (this.in_bsu_id) { 
        	  person.setBSUID(new String(ch, start, length));           
          }
          if (this.in_first_name) { 
        	  person.setFirstName(new String(ch, start, length));           
          }
          if (this.in_login_guid) { 
        	  person.setLoginGuid(new String(ch, start, length));           
          }
     }
} 
